package com.example.pc.day_9_14;

import android.graphics.Color;
import android.os.CancellationSignal;
import android.support.v4.hardware.fingerprint.FingerprintManagerCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private TextView tv_show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_show = findViewById(R.id.tv_show);
    }

    /**
     * 是否可以使用 指纹识别
     *
     * @param manager
     * @return
     */
    private boolean canUseFingerprint(FingerprintManagerCompat manager) {
        boolean hardwareDetected = manager.isHardwareDetected();
        Log.d(TAG, "canUseFingerprint: " + hardwareDetected);
        if (!hardwareDetected) // 手机是否有职位识别模块
            return false;
        return manager.hasEnrolledFingerprints();//是否已经录入指纹
    }

    public void test(View view) {
        tv_show.setTextColor(Color.BLACK);
        tv_show.setText("开始验证指纹...");
        //获取指纹管理对象
        FingerprintManagerCompat fingerprintManagerCompat = FingerprintManagerCompat.from(this);
        if (!canUseFingerprint(fingerprintManagerCompat)) {
            Log.d(TAG, "test: mei有指纹");
            return;
        }
        Log.i(TAG, "test: 有指纹");
        cancellationSignal = new android.support.v4.os.CancellationSignal();
        //开始验证指纹识别
        fingerprintManagerCompat.authenticate(new CryptoObjectHelper().buildCryptoObject(), 0, cancellationSignal, new FingerprintCallback(), null);
    }

    private android.support.v4.os.CancellationSignal cancellationSignal;

    /**
     * 取消按钮
     *
     * @param view
     */
    public void cancel(View view) {
        //指纹验证是否取消
        if (!cancellationSignal.isCanceled()) {
            //取消指纹验证
            cancellationSignal.cancel();
        }
    }

    private class FingerprintCallback extends FingerprintManagerCompat.AuthenticationCallback {
        /**
         * 当多次验证失败,该方法被调用,并且在将来的一段时间内不能在使用指纹验证.
         * (或被用户取消 )
         *
         * @param errMsgId
         * @param errString
         */
        @Override
        public void onAuthenticationError(int errMsgId, CharSequence errString) {
            super.onAuthenticationError(errMsgId, errString);
            switch (errMsgId) {
                case 5://取消验证
                    tv_show.setText("取消指纹验证:" + errMsgId + " , " + errString);
                    break;
                case 7://多次验证失败
                    tv_show.setText("多次验证失败:" + errMsgId + " , " + errString);
                    break;
            }
        }

        /**
         * 在验证过程中,遇到可恢复的异常时该方法被调用.(传感器脏，请把它擦干净。...)
         *
         * @param helpMsgId
         * @param helpString 提示信息
         */
        @Override
        public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
            super.onAuthenticationHelp(helpMsgId, helpString);
            tv_show.setText("指纹模块异常:" + helpMsgId + " , " + helpString);
        }

        /**
         * 指纹验证成功
         *
         * @param result
         */
        @Override
        public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {
            super.onAuthenticationSucceeded(result);
            tv_show.setText("指纹验证成功!");
            tv_show.setTextColor(Color.GREEN);
        }

        /**
         * 指纹验证失败
         */
        @Override
        public void onAuthenticationFailed() {
            super.onAuthenticationFailed();
            tv_show.setText("指纹验证失败!");
            tv_show.setTextColor(Color.RED);
        }
    }
}
